import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np

# 从 ./data/rank_score_table/2023_rank_score_table_science.csv 读取数据
df = pd.read_csv('./data/rank_score_table/2023_rank_score_table_science.csv')

# 数据格式类似：
"""
分值,人数,累计人数,名次
697,4,26,23
696,3,29,27
695,3,32,30
694,4,36,33
693,5,41,37
....
"""

# # 画出分数-人数的分布图，注意不使用中文
# plt.plot(df['分值'], df['人数'])
# plt.xlabel('score')
# plt.ylabel('number')
# plt.title('score distribution')


# # 画出对数正态分布的拟合：
# # 1. 计算对数正态分布的参数
# lognorm_params = stats.lognorm.fit(df['人数'])
# # 2. 画出拟合的对数正态分布
# pdf_fitted = stats.lognorm.pdf(df['人数'], *lognorm_params)
# plt.plot(df['分值'], pdf_fitted, 'r-')

# xmin, xmax = plt.xlim()

peoples = 12910000
data = np.repeat(df['分值'], df['人数'])
logdata = np.repeat(np.log(df['分值']), df['人数'])
print(np.log(data))

# 绘制原始数据的直方图
plt.hist(data, bins=df['分值'].size, density=True, alpha=0.6, color='g')
plt.show()
plt.cla()
plt.hist(logdata, bins=50, density=True, alpha=0.6, color='b')
plt.show()
# # 绘制拟合的概率密度函数
# xmin, xmax = plt.xlim()
# x = np.linspace(xmin, xmax, 100)
# shape, loc, scale = stats.lognorm.fit(data, floc=0)
# pdf = stats.truncnorm.pdf
# plt.plot(x, pdf, 'k', linewidth=2)
# title = "Fit results: shape = %.2f,  scale = %.2f" % (shape, scale)
# plt.title(title)
plt.show()